<template>
  <div class="flex flex-col h-full">
    <a-card>
      <div class="flex justify-between items-center">
        <a-button type="primary" class="mr-4" @click="router.go(-1)"> 返回 </a-button>
        <a-descriptions class="mt-3" :column="4">
          <a-descriptions-item label="单号" class="w-1/2"> {{ formState?.ckdh || '-' }}</a-descriptions-item>
          <a-descriptions-item label="出库仓库" class="w-1/2"> {{ formState?.ckmc || '-' }}</a-descriptions-item>
          <a-descriptions-item label="去向" class="w-1/2"> {{ formState?.qx || '-' }}</a-descriptions-item>
          <a-descriptions-item label="制单人" class="w-1/2"> {{ formState?.jbrname || '-' }}</a-descriptions-item>
          <a-descriptions-item label="制单时间" class="w-1/2"> {{ formState?.addtime || '-' }}</a-descriptions-item>
        </a-descriptions>
      </div>
    </a-card>
    <a-card class="mt-2 flex-1">
      <div class="flex justify-between items-center mb-2">
        <div class="flex items-center">
          <BaseInput
            v-model:value="tcCode"
            auto-mark
            :auto-focus="autoFocus"
            placeholder="请扫码"
            maxlength="20"
            class="w-64 mr-4"
            @change="onChangeCode"
            @keydown.enter="onSaveData"
          />
          <DrugInfo :value="{ mc: drugInfo.ypmc, gg: drugInfo.gg, cj: drugInfo.cj, ph: drugInfo.ph, yxq: drugInfo.expirationTime }" />
        </div>
        <div class="flex">
          <a-button type="primary" @click="openDelete">扫码移除</a-button>
        </div>
      </div>
      <BaseTable :data-source="tableData" column-code="000760" :loading="loading" :pagination="pagination" max-fill :extra-height="64">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'mc'">
            <MedicineInfo :value="record" />
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onOpenView(record)">查看追溯码</a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <DeleteTraceModal v-model:open="deleteOpen" mode="10" :row-data="{ ...formState, formSn: formState.ckdh }" @ok="getData()" />
    <ViewTrace v-model:open="viewTraceOpen" :row-data="{ ...rowData, formSn: rowData.ckdh }" />
  </div>
</template>

<script setup>
import { ref, watchEffect } from 'vue'
import { message } from 'ant-design-vue'
import { useRoute, useRouter } from 'vue-router'
import BaseTable from '@/components/BaseTable'
import { post } from '@/utils/request'
import DeleteTraceModal from '../components/DeleteTraceModal'
import ViewTrace from '../components/ViewTrace.vue'
import DrugInfo from '../components/DrugInfo.vue'

const route = useRoute()
const router = useRouter()
const formState = ref({})
const deleteOpen = ref(false)
const viewTraceOpen = ref(false)
const tcCode = ref('')
const rowData = ref({})
const drugInfo = ref({})
const autoFocus = ref(false)

const onSaveData = async () => {
  if (!tcCode.value) return
  autoFocus.value = false
  const { code, msg, data } = await post('/drugStorage/psiform/doSaveTcCodeDrug', {
    formSn: route.query?.ckdh,
    operationMode: 10,
    tcCode: tcCode.value,
    drugStoreId: formState.value.ckckbh
  })
  autoFocus.value = true
  tcCode.value = ''
  if (code !== 0) return message.error(msg || '操作失败')
  drugInfo.value = data || {}
  getData()
}

const getHeaderData = async () => {
  const { data, code } = await post('/drugStorage/psiform/getOutBoundHeadList', {
    dhmc: route.query.ckdh || '',
    pageNum: 1,
    pageSize: getSystemConfig('DFFAULT_PAGE_SHOW_COUNT') || 10
  })
  if (code === 0) {
    formState.value = data[0]
  }
}
const { loading, tableData, getData, pagination } = useTable({
  url: '/drugStorage/psiform/getOutBoundDetailList',
  immediate: false,
  formatParams: () => {
    return {
      ckdh: route.query?.ckdh
    }
  }
})
// const getData = async () => {
//   loading.value = true
//   const { data, code } = await post('/drugStorage/psiform/getOutBoundDetailList', {
//     ckdh: route.query.ckdh || '',
//     pageNum: 1,
//     pageSize: 999
//   })
//   loading.value = false
//   if (code === 0) {
//     dataSource.value = data
//   }
// }
const openDelete = (row) => {
  rowData.value = row
  deleteOpen.value = true
}
const onOpenView = (row) => {
  rowData.value = row
  viewTraceOpen.value = true
}
const onChangeCode = () => {
  drugInfo.value = {}
}

watchEffect(() => {
  if (route.query?.ckdh) {
    getHeaderData()
    getData()
  }
})
</script>
<style lang="less" scoped>
:deep(.ant-table-row-expand-icon) {
  display: none;
}
</style>
